package com.blb.ssm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.ssm.entity.Goods;
import com.blb.ssm.entity.Order;
import com.blb.ssm.entity.Order_goods;
import com.blb.ssm.service.OrderService;
import com.blb.ssm.service.TestService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private TestService testService;
	@Autowired
	private  OrderService orderService;
	
	@RequestMapping("")
	public String test(HttpSession session,String oid,String ostate,String otype) throws Exception {
	
			List<String> list=testService.getsupplier();
			session.setAttribute("supplier", list);
			List<Goods> list2=testService.getgoods();
			session.setAttribute("goods", list2);
			session.removeAttribute("list");
			return "file";
		
	}
	
	@RequestMapping("/ysh")
	public String test2(HttpSession session,String oid,String ostate,String otype) throws Exception {
		if(ostate.equals("1"))
		{
			List<Map<String, Object>> olist=orderService.getlist(ostate,otype,100);
			for(Map<String, Object> o: olist)
			{
				if(oid.equals(o.get("oid")))
				{
					session.setAttribute("obill", o.get("obill"));
					session.setAttribute("odate", o.get("odate"));
					session.setAttribute("osname", o.get("osname"));
					session.setAttribute("oname", o.get("oname"));
					session.setAttribute("onum", o.get("onum"));
					session.setAttribute("odepot", o.get("odepot"));
					session.setAttribute("oid", o.get("oid"));
					session.setAttribute("ostate", o.get("ostate"));
					session.setAttribute("otype", o.get("otype"));
				}
			}
			
			return "ysh";
		}
		else
		{
			List<Map<String, Object>> olist=orderService.getlist(ostate,otype,100);
			for(Map<String, Object> o: olist)
			{
				if(oid.equals(o.get("oid")))
				{
					session.setAttribute("obill", o.get("obill"));
					session.setAttribute("odate", o.get("odate"));
					session.setAttribute("osname", o.get("osname"));
					session.setAttribute("oname", o.get("oname"));
					session.setAttribute("onum", o.get("onum"));
					session.setAttribute("odepot", o.get("odepot"));
					session.setAttribute("oid", o.get("oid"));
					session.setAttribute("ostate", o.get("ostate"));
					session.setAttribute("otype", o.get("otype"));
				}
			}
			List<String> list=testService.getsupplier();
			session.setAttribute("supplier", list);
			List<Goods> list2=testService.getgoods();
			session.setAttribute("goods", list2);
			session.removeAttribute("list");
			return "wsh";
		}
		
	}
	
	@ResponseBody
	@RequestMapping("/lookysh")
	public  Map<String,Object> getOrderListAll(String oid,String ostate,String otype) throws Exception {
		Map<String, Object> map1 = new HashMap<String,Object>();
		List<Map<String, Object>> olist=orderService.getOnelist(ostate,otype,oid);
		map1.put("code", 0);
		map1.put("count", 1);
		map1.put("data", olist);
		return map1;
	}
	
	
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/bbb")
	public void getSupplierlist(HttpSession session, String m,String gname) throws Exception{
		JSONArray json=JSONArray.fromObject(m);
		Map<String, Object> map = (Map<String, Object>) session.getAttribute("list");
		List<Map<String,Object>> slist = new ArrayList<Map<String,Object>>();
		if (map!=null) {
			slist = (List<Map<String, Object>>) map.get("data");
		}else {
			map = new HashMap<String, Object>();
		}
		
		if(m !=null && json.size()!=0){
			slist.clear();
			for (int i=0; i<json.size(); i++) {
				JSONObject object = json.getJSONObject(i);
				JSONObject jsonObject = object.getJSONObject("value");
				Map<String, Object> bean = (Map<String, Object>) JSONObject.toBean(jsonObject, HashMap.class);
				slist.add(bean);
			}
		}
		
		List<Goods> list2 = (List<Goods>) session.getAttribute("goods");
		Goods goods=new Goods();
		for(Goods g:list2){
			if(g.getGname().equals(gname)){
				goods=g;
			}
		}
		session.setAttribute("goods02", goods);
		Map<String, Object> map1 = new HashMap<String,Object>();
		map1.put("oid", UUID.randomUUID().toString());
		map1.put("gname", goods.getGname());
		map1.put("gtype", goods.getGtype());
		map1.put("gnorms", goods.getGnorms());
		map1.put("num", "");
		map1.put("gunit", goods.getGunit());
		map1.put("price", "");
		map1.put("money", 0);
		map1.put("gbrand", goods.getGbrand());
		map1.put("gcode", goods.getGcode());
		map1.put("msg", "");
		map1.put("ostate",0);
		slist.add(map1);
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", 1);
		map.put("data", slist);
		session.setAttribute("list", map);
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/sss")
	public Map<String,Object> get(HttpSession session){
		Object object = session.getAttribute("list");
		if (object==null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("code", 0);
			map.put("msg", "");
			map.put("count", 1);
			map.put("data", new ArrayList<>());
			return map;
		}
		return (Map<String, Object>) session.getAttribute("list");
	}
	
	@ResponseBody
	@RequestMapping("/shrk")
	public String saveShrk(String oid,String ostate){
		ostate = "1";
		//修改数据
		testService.saveShrk(oid,ostate);
		return "ok";
	}
	
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/save")
	public String saveTable(String sign,HttpSession session,Order order){
		Map<String, Object> map = (Map<String, Object>) session.getAttribute("list");
		List<Map<String,Object>> lists= (List<Map<String, Object>>) map.get("data");
		Goods goods = (Goods) session.getAttribute("goods02");
		Order_goods og=new Order_goods();
		for(Map<String,Object> m:lists){
			if(sign.equals("1")){
				m.put("ostate", 1);
			}
			order.setOstate(Integer.parseInt(m.get("ostate").toString()));
			order.setOmsg((String)m.get("omsg"));
			og.setOid(m.get("oid").toString());
			og.setNum(Integer.parseInt(m.get("num").toString()));
			og.setPrice(Double.parseDouble(m.get("price").toString()));
			og.setMoney(Double.parseDouble(m.get("money").toString()));
			og.setMsg(m.get("msg").toString());
			order.setOid(m.get("oid").toString());
		}
		og.setGid(goods.getGid());
		order.setOtype(1);
		//添加orders表数据
		testService.addOrder(order);
		//添加order_goods数据
		testService.addOrder_goods(og);
		return "ok";
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/del")
	public void dels(String oid,HttpSession session){
		Map<String, Object> map = (Map<String, Object>) session.getAttribute("list");
		List<Map<String,Object>> lists= (List<Map<String, Object>>) map.get("data");
		for(int i=0;i<lists.size();i++){
			if(lists.get(i).get("oid").equals(oid)){
				lists.remove(i);
			}
		}
	}
}
